//
//  ZhuihaoHeaderView.m
//  SSC
//
//  Created by 红沙尘 on 17/6/24.
//  Copyright © 2017年 红沙尘. All rights reserved.
//

#import "ZhuihaoHeaderView.h"

@interface ZhuihaoHeaderView ()<UITextFieldDelegate>
{
    __weak IBOutlet NSLayoutConstraint *lineA;
    __weak IBOutlet NSLayoutConstraint *lineB;
    
    __weak IBOutlet UIView *bgview;
    __weak IBOutlet UIView *viewA;
    __weak IBOutlet UIView *viewB;
    __weak IBOutlet UIView *viewC;
    __weak IBOutlet UILabel *numLabel;
    
    __weak IBOutlet UIButton *btn2;
    __weak IBOutlet UIButton *btn5;
    __weak IBOutlet UIButton *btn10;
    __weak IBOutlet UIButton *btn15;
    
    __weak IBOutlet UITextField *shoudongField;
    
    __weak IBOutlet UIView *bgView;
    __weak IBOutlet UITextField *startQiField;
    
    __weak IBOutlet UIButton *zuihaoBtn;
    
    NSDictionary *_qiDict;
    
    NSInteger _qiCount;
    NSString *_qishiNumber;
    NSString *_beishuNumber;
    
    NSInteger startIndex;
}

@property (nonatomic, strong) NSArray *qiArray;
@property (nonatomic, assign) CaipiaoType otype;

@end

@implementation ZhuihaoHeaderView

+ (ZhuihaoHeaderView *)viewWithDelegate:(id <ZhuihaoHeaderViewDelegate>)delegate type:(CaipiaoType)otype {
    
    ZhuihaoHeaderView *headView = [[[NSBundle mainBundle] loadNibNamed:@"ZhuihaoHeaderView" owner:nil options:nil] objectAtIndex:0];
    headView.delegate = delegate;
    headView.otype = otype;
    
    return headView;
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)awakeFromNib {
    
    [super awakeFromNib];

    [self setupUI];
    
    [self qiButtonPressed:btn5];
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(resetQueryFutrueList:)
                                                 name:@"ResetQueryFutrueList"
                                               object:nil];
}

- (void)resetQueryFutrueList:(NSNotification *)info {
    
    [self queryFutrueList];
}

- (void)setOtype:(CaipiaoType)otype {
    _otype = otype;
    
    [self queryFutrueList];
}

- (void)queryFutrueList {
    
    [SSCRequest queryFutrueListIssueWithType:self.otype success:^(NSDictionary *responseObject) {
        
        self.qiArray = responseObject[@"data"];
        
        [self selectCardAtIndex:0];
        
    } fail:^(NSError *error) {
        
    }];
}

#pragma mark - IBAciton

- (IBAction)jian {
    int num = [numLabel.text intValue];
    
    num --;
    
    num = MAX(num, 1);
    
    numLabel.text = [NSString stringWithFormat:@"%d",num];
}

- (IBAction)jia {
    int num = [numLabel.text intValue];
    
    num ++;
    
    numLabel.text = [NSString stringWithFormat:@"%d",num];
}

- (IBAction)qiButtonPressed:(UIButton *)button {

    btn2.backgroundColor  = [UIColor whiteColor];
    btn5.backgroundColor  = [UIColor whiteColor];
    btn10.backgroundColor = [UIColor whiteColor];
    btn15.backgroundColor = [UIColor whiteColor];
    
    button.backgroundColor = COLOR_FF5253;
    _qiCount = button.tag;
    shoudongField.text = [NSString stringWithFormat:@"%d",(int)_qiCount];
}

- (IBAction)zhuihaoBtn {
    
    if (_qiCount == 0) {
        [MTool showMessage:@"选择追号多少期" inView:nil];
        return;
    }
    
    if (startQiField.text.length == 0) {
        [MTool showMessage:@"选择起始期号" inView:nil];
        return;
    }
    // 5  
    if (startIndex + _qiCount > _qiArray.count) {
        [MTool showMessage:@"超出最大可追加的期次" inView:nil];
        return;
    }
    
    if ([self.delegate respondsToSelector:@selector(shengchengzuihaoWithQi:qiNumber:beishu:array:startIndex:)]) {
        
        [self.delegate shengchengzuihaoWithQi:_qiCount
                                     qiNumber:startQiField.text
                                       beishu:numLabel.text
                                        array:_qiArray
                                   startIndex:startIndex];
    }
}

- (IBAction)qishiButtonPressed {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"起始期号"
                                                                   message:nil
                                                            preferredStyle:UIAlertControllerStyleActionSheet];
    for (int i = 0; i < _qiArray.count; i ++) {
        NSDictionary *dict = _qiArray[i];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:dict[@"issue"]
                                                         style:UIAlertActionStyleDefault
                                                       handler:^(UIAlertAction * _Nonnull action)
                                 {
                                     [self selectCardAtIndex:i];
                                 }];
        [alert addAction:action];
    }
    
    [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
    
    [[self viewController] presentViewController:alert animated:YES completion:nil];
}

- (void)shoudongTextFieldChanged:(UITextField *)textField {
    
    UIButton *btn = [[UIButton alloc] init];
    btn.tag = [textField.text intValue];
    
    switch (btn.tag) {
        case 2:
            btn = btn2;
            break;
        case 5:
            btn = btn5;
            break;
        case 10:
            btn = btn10;
            break;
        case 15:
            btn = btn15;
            break;
        default:
            break;
    }
    
    [self qiButtonPressed:btn];
}

- (void)setLayer:(UIView *)view {
    view.layer.cornerRadius = 2;
    view.layer.masksToBounds = YES;
    view.layer.borderColor = COLOR_CCCCCC.CGColor;
    view.layer.borderWidth = LineHeight;
}

- (void)selectCardAtIndex:(NSInteger)index {
    
    if (_qiArray.count == 0) return;
    
    _qiDict = [_qiArray[index] copy];
    
    startQiField.text = _qiDict[@"issue"];
    
    startIndex = index;
}

- (UIViewController *)viewController {
    for (UIView* next = [self superview]; next; next = next.superview) {
        UIResponder* nextResponder = [next nextResponder];
        if ([nextResponder isKindOfClass:[UINavigationController class]]) {
            
            return (UIViewController *)nextResponder;
        }
    }
    
    return nil;
}

- (void)setupUI {
    
    lineA.constant = LineHeight;
    lineB.constant = LineHeight;
    
    viewA.layer.borderColor = COLOR_CCCCCC.CGColor;
    viewA.layer.borderWidth = LineHeight;
    viewB.layer.borderColor = viewA.layer.borderColor;
    viewB.layer.borderWidth = viewA.layer.borderWidth;
    viewC.layer.borderColor = viewA.layer.borderColor;
    viewC.layer.borderWidth = viewA.layer.borderWidth;
    
    bgview.layer.borderColor = COLOR_CCCCCC.CGColor;
    bgview.layer.borderWidth = LineHeight;
    
    zuihaoBtn.layer.cornerRadius  = 2;
    zuihaoBtn.layer.masksToBounds = YES;
    
    [self setLayer:btn2];
    [self setLayer:btn5];
    [self setLayer:btn10];
    [self setLayer:btn15];
    
    bgView.layer.borderColor = COLOR_CCCCCC.CGColor;
    bgView.layer.borderWidth = LineHeight;
    
    [self setLayer:shoudongField];
    shoudongField.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, 40)];
    shoudongField.leftViewMode = UITextFieldViewModeAlways;
    
    [self setLayer:startQiField];
    UILabel *rightLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 20, 40)];
    rightLabel.text = @"▼";
    rightLabel.textColor = COLOR_999999;
    startQiField.rightView = rightLabel;
    startQiField.rightViewMode = UITextFieldViewModeAlways;
    
    [shoudongField addTarget:self
                      action:@selector(shoudongTextFieldChanged:)
            forControlEvents:UIControlEventEditingChanged];
}


@end
